package com.kittlen.managementSystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kittlen.managementSystem.entity.MySqlLimit;
import com.kittlen.managementSystem.entity.Result;
import com.kittlen.managementSystem.entity.VVehicle;
import com.kittlen.managementSystem.service.IVVehicleService;
import com.kittlen.managementSystem.utils.ObjectUtil;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 车辆表,包含教练(教练主) 前端控制器
 * </p>
 *
 * @author kittlen
 * @since 2021-02-02
 */
@RestController
@RequestMapping("/vVehicle")
public class VVehicleController {

    @Resource
    IVVehicleService ivVehicleService;

    @RequestMapping("/selects")
    public Result selects(VVehicle vVehicle, @RequestParam(required = false, defaultValue = "0") int page,
                          @RequestParam(required = false, defaultValue = "0") int limit, @RequestParam(required = false,defaultValue = "0")int isFree) {
        Result result = new Result();
        QueryWrapper<VVehicle> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNoEmpty(vVehicle.getVehicleId())){
            queryWrapper.like("vehicle_id", vVehicle.getVehicleId());
        }
        if(ObjectUtil.isNoEmpty(vVehicle.getVehicleName())){
            queryWrapper.like("vehicle_name", vVehicle.getVehicleName());//车名
        }
        if(ObjectUtil.isNoEmpty(vVehicle.getLicensePlateNumber())){
            queryWrapper.like("license_plate_number", vVehicle.getLicensePlateNumber());//车牌
        }
        if(ObjectUtil.isNoEmpty(vVehicle.getType())){
            queryWrapper.eq("type", vVehicle.getType());//档位类型
        }
        if(ObjectUtil.isNoEmpty(vVehicle.getCoachId())){
            queryWrapper.eq("coach_id", vVehicle.getCoachId());//教练ID
        }
        if (page!=-1) {
            MySqlLimit mySqlLimit=new MySqlLimit(page,limit);
            queryWrapper.last("LIMIT "+mySqlLimit.getOffset()+","+mySqlLimit.getRows());
        }
        if(ObjectUtil.isNoEmpty(isFree)&&isFree!=0){//查询未被分派的车辆
            queryWrapper.isNull("coach_id");
        }
        List<VVehicle> list=ivVehicleService.list(queryWrapper);
        result.setData(list);
        queryWrapper.last("");
        result.setCount(ivVehicleService.count(queryWrapper));
        return result;
    }

}
